import Vue from 'vue'
import VueRouter from 'vue-router'
const Login = () =>
  import(/* webpackChunkName:"login_home_welcome" */ '../components/Login.vue')
// import Login from '../components/Login'
const Home = () =>
  import(/* webpackChunkName:"login_home_welcome" */ '../components/Home.vue')
// import Home from '../components/Home'
const Welcome = () =>
  import(
    /* webpackChunkName:"login_home_welcome" */ '../components/Welcome.vue'
  )
// import Welcome from '../components/Welcome'
const Users = () =>
  import(/* webpackChunkName:"users" */ '../components/Users.vue')
// import Users from '../components/Users'
const Rights = () =>
  import(/* webpackChunkName:"rights" */ '../components/rights/Rights.vue')
// import Rights from '../components/rights/Rights'
const Roles = () =>
  import(/* webpackChunkName:"roles" */ '../components/rights/Roles.vue')
// import Roles from '../components/rights/Roles'
const Goods = () =>
  import(/* webpackChunkName:"goods" */ '../components/goods/Goods.vue')
// import Goods from '../components/goods/Goods'
const AddGoods = () =>
  import(/* webpackChunkName:"addgoods" */ '../components/goods/AddGoods.vue')
// import AddGoods from '../components/goods/AddGoods'
const Params = () =>
  import(/* webpackChunkName:"params" */ '../components/goods/Params.vue')
// import Params from '../components/goods/Params'
const Categories = () =>
  import(
    /* webpackChunkName:"categories" */ '../components/goods/Categories.vue'
  )
// import Categories from '../components/goods/Categories'
const Orders = () =>
  import(/* webpackChunkName:"orders" */ '../components/Orders.vue')
// import Orders from '../components/Orders'
const Reports = () =>
  import(/* webpackChunkName:"reports" */ '../components/Reports.vue')
// import Reports from '../components/Reports'

Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: 'login' },
  { path: '/login', component: Login },
  {
    path: '/home',
    component: Home,
    children: [
      { path: '/home', redirect: '/welcome' },
      { path: '/welcome', component: Welcome },
      { path: '/users', component: Users },
      { path: '/rights', component: Rights },
      { path: '/roles', component: Roles },
      { path: '/goods', component: Goods },
      { path: '/goods/addgoods', component: AddGoods },
      { path: '/params', component: Params },
      { path: '/categories', component: Categories },
      { path: '/orders', component: Orders },
      { path: '/reports', component: Reports }
    ]
  }
]
// const originalPush = VueRouter.prototype.push
// VueRouter.prototype.push = function push(location) {
//   return originalPush.call(this, location).catch(err => err)
// }
const router = new VueRouter({
  routes
})
// 导航守卫
router.beforeEach((to, from, next) => {
  if (to.path === '/login') {
    next() // 在登录页不需要token直接放行
  } else {
    const mytoken = sessionStorage.getItem('login_token')
    if (!mytoken) {
      return next('/login') // 没有token 被拦截到登录页
    }
    next()
  }
})
export default router
